package com.xinghanlaser.applet.points;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.xinghanlaser.applet.base.flow.core.BasePoint;
import com.xinghanlaser.applet.entity.TopicBind;
import com.xinghanlaser.applet.entity.TopicDetail;
import com.xinghanlaser.applet.entity.TopicHandleDetail;
import com.xinghanlaser.applet.service.*;
import com.xinghanlaser.applet.vo.applet.ProductDetailVO;
import com.xinghanlaser.applet.vo.point.TopicPostVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

import static com.xinghanlaser.applet.base.auth.constant.AdminConstant.SUPER_ADMIN_ROLE_ID;
import static com.xinghanlaser.applet.constant.AdminConstant.AFTER_SALES_SUPERVISOR_ROLE_ID;
import static com.xinghanlaser.applet.constant.AdminConstant.DEALER_ROLE_ID;


/**
 * <p>
 *
 * <p>
 *
 * @author: mftuo 陀铭峰
 * @since: 2024/5/11 14:24
 */
@Component
public class AddTopicPoint extends BasePoint<TopicPostVO> {
    @Resource
    private ITopicService topicService;

    @Resource
    private IDealerSecretKeyService dealerService;

    @Resource
    private ITopicDetailService detailService;

    @Resource
    private ITopicBindService bindService;

    @Resource
    private ITopicHandleDetailService handleService;

    @Resource
    private ITopicHandleHistoryService historyService;

    @Override
    @DSTransactional
    protected void pointOperation(TopicPostVO postVO) {
        final String productSn = postVO.getProductSn();

        // 根据序列号获取产品详情
        ProductDetailVO productDetail = topicService.getProductDetail(productSn);

        // 获取经销商信息
        final String dealerName = productDetail.getCustomerName();
        final Long dealerId = dealerService.getDealerIdByName(dealerName);

        // 写入产品信息
        TopicDetail topicEntity = BeanUtil.copyProperties(postVO, TopicDetail.class);
        topicEntity.setProductSpec(productDetail.getProductSpec())
                .setProductStatus(productDetail.getStatus())
                .setDeliverTime(productDetail.getDeliverTime())
                .setCustomerName(dealerName)
                .setCustomerId(dealerId);

        // 保存申请单详情
        detailService.save(topicEntity);

        // 保存处理流程信息
        TopicHandleDetail handleEntity = new TopicHandleDetail();
        handleEntity.setTopicId(topicEntity.getId())
                .setCurrentPointId(getPointId());
        handleService.save(handleEntity);

        // 保存处理流程历史信息
        historyService.saveHistory(
                handleEntity.getId(),
                getPointId(),
                "[" + postVO.getContactUser()+ "]已提交申请",
                null
        );

        // 保存相关人信息
        this.bindTopic(topicEntity, dealerId);

    }

    @Override
    protected Class<TopicPostVO> getPointDataType() {
        return TopicPostVO.class;
    }

    @Override
    public Long getPointId() {
        return 1L;
    }

    private void bindTopic(TopicDetail topicEntity, Long dealerId){
        final Long topicId = topicEntity.getId();

        Set<TopicBind> insert = new HashSet<>();

        // 绑定售后主管
        insert.add(new TopicBind()
                .setTopicId(topicId)
               .setRoleId(AFTER_SALES_SUPERVISOR_ROLE_ID)
        );

        // 绑定特定经销商
        insert.add(new TopicBind()
                .setTopicId(topicId)
                .setRoleId(DEALER_ROLE_ID)
                .setUserId(dealerId)
        );

        // 绑定超级管理员
        insert.add(new TopicBind()
                .setTopicId(topicId)
                .setRoleId(SUPER_ADMIN_ROLE_ID)
        );

        bindService.saveBatch(insert);
    }
}
